<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/25
 * Time: 8:36
 */
namespace app\admin\model;
use think\facade\Request;
use think\Model;
use think\model\concern\SoftDelete;

class Goods extends Model
{
    use SoftDelete;
    protected $deleteTime='delete_time';
    public function searchIsShowAttr($q,$v,$d)
    {
        if (isset($v)&&$v != '')    $q->where('is_show','=',$v);
    }
    public function searchIsHotAttr($q,$v,$d){
        if (isset($v)&&$v != '')    $q->where('is_hot','=',$v);
    }
    public function searchIsDownAttr($q,$v,$d){
        if (isset($v)&&$v != '')    $q->where('is_down','=',$v);
    }
    public function searchCidAttr($q,$v,$d){
        if (isset($v)&&$v != '')    $q->where('cid','=',$v);
    }
    public  function searchKeywordsAttr($q,$v,$d){
        if (isset($v)&&$v != '')    $q->where('g.name','like',"%{$v}%");
    }
    public static function getGoodList($where,$keywords)
    {
        $list=Goods::field('g.id,g.name,g.photo_x,g.price,c.name cname,g.addtime,g.is_show,g.is_down,g.is_hot')
            ->alias('g')
            ->join('category c','c.id=g.cid')
            ->withSearch(array_keys($where),$where)
            ->paginate([
                'list_rows'=>5,
                'query'=>$where
            ])
            ->each(function ($item,$key) use($keywords){
            $new_key="<span style='color: red;'>{$keywords}</span>";
            $item['name']=str_replace($keywords,$new_key,$item['name']);
        });
        return  $list;
    }
    public static function  getSoftDeleteGoodList($where,$keywords){
        $list=Goods::onlyTrashed()
            ->field('g.id,g.name,g.photo_x,g.price,c.name cname,g.addtime,g.is_show,g.is_down,g.is_hot')
            ->alias('g')
            ->join('category c','c.id=g.cid')
            ->withSearch(array_keys($where),$where)
            ->paginate([
                'list_rows'=>5,
                'query'=>$where
            ])
            ->each(function ($item,$key) use($keywords){
                $new_key="<span style='color: red;'>{$keywords}</span>";
                $item['name']=str_replace($keywords,$new_key,$item['name']);
            });
        return  $list;
    }
}